<?php
/**
 * 中奖信息导出
 * User: cxm
 * Date: 2020/2/10
 * Time: 9:50
 */
namespace App\Excel;

use EasyWeChat\Kernel\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;

class HistoryExport implements FromCollection
{
    /**
     * 导出数据
     * @var array
     */
    protected $data=array();

    public function __construct($data){
        $this->data = $data;
    }

    //数组转集合
    public function collection()
    {
        return new Collection($this->createData());
    }
    //业务代码
    public function createData()
    {
        $data[] = ["姓名","手机","工会服务卡号","奖品名称","奖品分类名称","收货人手机","联系人","地址","中奖时间"];
        foreach ( $this->data as $key=>$item) {
            $address = '';
            if (isset($item['draw_data']) && isset($item['draw_data']['province'])) {
                $address = $item['draw_data']['province']. ' '. $item['draw_data']['city'].' '.$item['draw_data']['district'].' '. $item['draw_data']['address'];
            }
            $data[] = [
                $item['user']['name'],
                $item['user']['mobile'],
                '\''.$item['user']['card_no'],
                $item['goods']['name'],
                $item['award_name'],
                $item['draw_data']['mobile'] ?? "",
                $item['draw_data']['username'] ?? '',
                $address,
                $item['created_at']
            ];
        }
        return $data;
    }
}